﻿#define _CRT_SECURE_NO_WARNINGS
//作者：爱吃糖的boy
#include "game.h"
void InitBoard(char arr[ROW][COLS], int rows, int cols, char set)
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0;j< cols;j++) 
		{
			arr[i][j] = set;
		}
	}
}
void DisplayBoard(char arr[ROWS][COLS], int row, int col) 
{
	printf("--------扫雷游戏-------\n");
	for (int i = 0; i <= col; i++)
	{
		printf("%2d ", i);
	}
	printf("\n");
	for (int i = 1; i <= row; i++)
	{
		printf("%2d ", i);
		for (int j = 1; j <= col; j++)
		{
			printf("%2c ",arr[i][j]);
		}
		printf("\n");
	}
}

void SetMine(char arr[ROWS][COLS],int row,int col)
{
	//布置10个雷
	//⽣成随机的坐标，布置雷
	int count = EASY_COUNT;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (arr[x][y] == '0')
		{
			arr[x][y] = '1';
			count--;
		}
	}
}
void FindMine(char arr[ROWS][COLS], char arr2[ROWS][11], int row, int col)
{

	int x = 0;
	int y = 0;
	int win = 0;
	while (win < row * col - 9)
	{
		printf("请输入要排查的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (arr[x][y] == '1')
			{
				printf("很遗憾，你被炸死了\n");
				DisplayBoard(arr, 9,9 );
				break;
			}
			else
			{
				//该位置不是雷，就统计这个坐标周围有⼏个雷
				int count = GetMineCount(arr, x, y);
				arr2[x][y] = count + '0';
				DisplayBoard(arr2, 10, 10);
				win++;
			}
		}
		else
		{
			printf("坐标非法，重新输⼊\n");
		}
	}
	if (win == row * col - EASY_COUNT)
	{
		printf("恭喜你，排雷成功\n");
		DisplayBoard(arr,ROW, COL);
	}
}
int GetMineCount(char mine[ROWS][COLS], int x, int y) 
{

	return (mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x+1][y]+
		mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0');
}